package com.simple.gateway.center.domian.auth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.simple.gateway.center.application.IApplicationManageService;
import com.simple.gateway.center.application.IAuthService;
import com.simple.gateway.center.domian.application.dto.ApplicationInterfaceMethodDTO;
import com.simple.gateway.center.domian.auth.bo.GatewayUserBO;
import com.simple.gateway.center.domian.auth.dto.AuthTokenInfoDTO;
import com.simple.gateway.center.domian.auth.param.AuthTokenParam;
import com.simple.gateway.center.domian.auth.param.BindInterfacesParam;
import com.simple.gateway.center.domian.auth.param.ChangeAuthParam;
import com.simple.gateway.center.domian.auth.param.GatewayUserFullParam;
import com.simple.gateway.center.domian.auth.repository.IAuthRepository;
import com.simple.gateway.center.web.param.ApplicationCompositeParam;
import com.simple.gateway.common.util.TokenGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目: simple-gateway-core
 *
 * 功能描述:
 *
 * @author: WuChengXing
 *
 * @create: 2023-08-29 23:54
 **/
@Service
public class AuthServiceImpl implements IAuthService {

    @Autowired
    private IAuthRepository iAuthRepository;

    @Resource
    private IApplicationManageService applicationManageService;

    @Override
    public Boolean registerUser(GatewayUserFullParam gatewayUserFullParam) {
        return iAuthRepository.registerUser(GatewayUserBO.param2Bo(gatewayUserFullParam));
    }

    @Override
    public Boolean bindInterfaces(BindInterfacesParam bindInterfacesParam) {
        ApplicationCompositeParam param = new ApplicationCompositeParam();
        if (CollectionUtil.isEmpty(bindInterfacesParam.getInterfaceMethodIds())) {
            if (StrUtil.isNotBlank(bindInterfacesParam.getSystemId())) {
                param.setSystemId(bindInterfacesParam.getSystemId());
            }
            if (StrUtil.isNotBlank(bindInterfacesParam.getInterfacesId())) {
                param.setInterfaceId(bindInterfacesParam.getInterfacesId());
            }
            List<ApplicationInterfaceMethodDTO> applicationInterfaceMethodDTOS = applicationManageService.listApplicationMethod(param);
            bindInterfacesParam.setInterfaceMethodIds(applicationInterfaceMethodDTOS.stream().map(ApplicationInterfaceMethodDTO::getId).collect(Collectors.toList()));
        }
        return iAuthRepository.bindInterfaces(bindInterfacesParam);
    }

    @Override
    public Boolean changeAuthInto(ChangeAuthParam changeAuthParam) {
        return iAuthRepository.changeAuthInto(changeAuthParam);
    }

    @Override
    public AuthTokenInfoDTO generateToken(AuthTokenParam authTokenParam) {
        String userKey = authTokenParam.getUserKey();
        Boolean hasUserInSystem = iAuthRepository.hasUserInSystem(userKey);
        if (hasUserInSystem) {
            return AuthTokenInfoDTO.builder()
                    .userKey(authTokenParam.getUserKey())
                    .ttlMillis(authTokenParam.getTtlMillis())
                    .token(TokenGenerateUtil.generateToken(authTokenParam.getUserKey(), authTokenParam.getTtlMillis()))
                    .build();
        } else {
            return AuthTokenInfoDTO.builder()
                    .userKey(authTokenParam.getUserKey())
                    .ttlMillis(authTokenParam.getTtlMillis())
                    .token("该用户不存在，请注册用户先，userKey == userId")
                    .build();
        }
    }
}
